package city.spring.modules.medicine.service.impl;

import city.spring.modules.medicine.entity.MedicineEntity;
import city.spring.modules.medicine.repository.MedicineRepository;
import city.spring.modules.medicine.service.MedicineService;
import city.spring.modules.medicine.vo.MedicineVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service：药品信息
 *
 * @author 谢金婷
 * @date 2020-06-19 10:08:21
 */
@CacheConfig(cacheNames = {MedicineServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class MedicineServiceImpl extends ServiceImpl<MedicineRepository, MedicineEntity> implements MedicineService {

    private final MedicineRepository medicineRepository;

    public MedicineServiceImpl(MedicineRepository medicineRepository) {
        this.medicineRepository = medicineRepository;
    }

    @Override
    public void saveMedicine(MedicineEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateMedicine(MedicineEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteMedicine(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteMedicine(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public IPage<MedicineEntity> getMedicineNowarehousing(Pageable pageable) {
        IPage<MedicineEntity> page = MyBatisUtils.toPage(pageable);
        return medicineRepository.getMedicineNowarehousing(page, lambdaQuery().getWrapper());
    }

    @Override
    public List<MedicineEntity> getMedicineByIswarehousing() {
        return medicineRepository.getMedicineByIswarehousing();
    }

    @Override
    public MedicineEntity getById(String id) {
        return medicineRepository.getById(id);
    }

    @Override
    public IPage<MedicineVO> listPage(Page<? extends MedicineEntity> page, Wrapper<? extends MedicineEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }

    @Override
    public IPage<MedicineVO> getMedicineIswarehousingPage(Page<? extends MedicineEntity> page, Wrapper<? extends MedicineEntity> wrapper) {
        return baseMapper.getMedicineIswarehousingPage(page, wrapper);
    }
}